package org.example.Polls;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.DateConverter;
import com.thoughtworks.xstream.converters.collections.CollectionConverter;
import com.thoughtworks.xstream.converters.javabean.JavaBeanConverter;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XStreamManager {
	
	private static XStreamManager instance = null;
	
	private XStream xstream;
	
	private XStreamManager() {
		xstream = new XStream(new DomDriver()); // does not require XPP3 library
		xstream.registerConverter(new CollectionConverter(xstream.getMapper()));
		xstream.alias("user", User.class);
		xstream.alias("poll", Poll.class);
		xstream.alias("participant", PollParticipant.class);
		xstream.alias("item", PollItem.class);
		xstream.addImplicitCollection(Poll.class, "participants", "participants", PollParticipant.class);
		xstream.addImplicitCollection(Poll.class, "items", "items", PollItem.class);
		String[] dateFormats = new String[] { "yyyy-MM-dd", "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss.SZ", "yyyy-MM-dd'T'HH:mm:ss"};
		xstream.registerConverter(new DateConverter("yyyy-MM-dd'T'HH:mm:ss", dateFormats));
	}
	
	public XStream getXStream() {
		return xstream;
	}
	
	public static synchronized XStreamManager getInstance() {
		if (instance == null) {
			instance = new XStreamManager();
		}
		return instance;
	}
}
